package com.voice.call.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.voice.call.common.Const;
import com.voice.call.common.Result;
import com.voice.call.entity.VoiceRate;
import com.voice.call.mapper.VoiceRateMapper;
import com.voice.call.service.VoiceRateService;

@Service
public class VoiceRateServiceImpl implements VoiceRateService{
	
	@Resource
	private VoiceRateMapper voiceRateMapper;

	@Override
	public Result getVoiceRateList(){
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", Const.Y);
		List<VoiceRate> list = voiceRateMapper.list(params);
		
		Map<String, List<Map<String, Object>>> res = new HashMap<String, List<Map<String, Object>>>();
		List<Map<String, Object>> hotList = new ArrayList<Map<String, Object>>();
		if(list!=null && !list.isEmpty()){
			for(VoiceRate vr : list){
				String key = vr.getRegion().getEnName().substring(0, 1);
				List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
				if(res.get(key)!=null){
					mapList=res.get(key);
				}else{
					res.put(key, mapList);
				}
				Map<String, Object> resObj = new HashMap<String, Object>();
				resObj.put("name",vr.getRegion().getEnName());
				resObj.put("sort", vr.getSort());
				resObj.put("rate", vr.getRate()/100.0);
				resObj.put("prefix", vr.getCallPrefix());
				resObj.put("isHot", vr.getIsHot());
				resObj.put("country_pic", vr.getRegion().getCountryPic());
				if(Const.Y.equals(vr.getIsHot())){
					if(res.get("hot")!=null){
						hotList = res.get("hot");
					}else{
						res.put("hot", hotList);
					}
					hotList.add(resObj);
				}
				
				res.get(key).add(resObj);
			}
			//res.put("hot", hotList);
			//log.info("排序前:{}",JSONUtil.objectToJson(hotList));
			bubbleSort(hotList);
			//log.info("排序后:{}",JSONUtil.objectToJson(hotList));
		}
		return new Result(res);
	}
	
	@Override
	public Result getSearchVoiceRateList(String keyword,String lang){
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", Const.Y);
		params.put("keyword", keyword.toLowerCase());
		params.put("lang", lang);
		List<VoiceRate> list = voiceRateMapper.searchVoiceRateList(params);
		List<Map<String, Object>> rsList = new ArrayList<Map<String, Object>>();
		if(list!=null && !list.isEmpty()){
			for(VoiceRate vr : list){
				Map<String, Object> resObj = new HashMap<String, Object>();
				resObj.put("name",vr.getRegion().getName());
				resObj.put("sort", vr.getSort());
				resObj.put("rate", vr.getRate());
				resObj.put("prefix",Integer.valueOf(vr.getRegion().getCode()));
				//resObj.put("prefix",vr.getCallPrefix().replaceAll("^00", ""));
				resObj.put("isHot", vr.getIsHot());
				resObj.put("country_pic", vr.getRegion().getCountryPic());
				rsList.add(resObj);
			}
		}
		
		return new Result(rsList);
	}
	
	@Override
	public Result getHotVoiceRateList(String lang){
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("status", Const.Y);
		params.put("isHot", Const.Y);
		params.put("lang", lang);
		List<VoiceRate> list = voiceRateMapper.list(params);
		List<Map<String, Object>> rsList = new ArrayList<Map<String, Object>>();
		if(list!=null && !list.isEmpty()){
			for(VoiceRate vr : list){
				Map<String, Object> resObj = new HashMap<String, Object>();
				resObj.put("name",vr.getRegion().getName());
				resObj.put("sort", vr.getSort());
				resObj.put("rate", vr.getRate());
				resObj.put("prefix", Integer.valueOf(vr.getRegion().getCode()));
				resObj.put("isHot", vr.getIsHot());
				resObj.put("country_pic", vr.getRegion().getCountryPic());
				rsList.add(resObj);
			}
		}
		
		return new Result(rsList);
	}
	
	//冒泡法排序(升序) 
    private  void bubbleSort(List<Map<String, Object>> numbers) {   
    	Map<String, Object> temp; // 记录临时中间值   
        int size = numbers.size(); // 数组大小   
        for (int i = 0; i < size - 1; i++) {   
            for (int j = i + 1; j < size; j++) {   
                if (Integer.valueOf(numbers.get(i).get("sort").toString()) > Integer.valueOf(numbers.get(j).get("sort").toString())) { // 交换两数的位置   
                    temp = numbers.get(i);
                    numbers.set(i, numbers.get(j));
                    numbers.set(j, temp);
                }   
            }   
        }   
    }  
}
